package com.tfswx.ruleengine.fn;

import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import com.tfswx.ruleengine.compute.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateFrom extends BaseFn {

    private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");

    @Override
    public Object call(Object[] arguments, FelContext context) {
        String dateStr = (String) arguments[0];
        if (StringUtils.isBlank(dateStr)) {
            return null;
        } else {
            Matcher matcher = DATE_PATTERN.matcher(dateStr);
            if (!matcher.find()) {
                throw new AppException("dateStr日期格式不满足2000-01-01的格式");
            } else {
                dateStr = matcher.group(1);
                Date parse = DateUtil.parse(dateStr + " 00:00:00");
                return DateUtil.format(parse, "yyyy年MM月dd日");
            }
        }
    }

    @Override
    public String getZwmc() {
        return "日期转中文日期";
    }


    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("dateStr"));
        return params;
    }

    @Override
    public String getName() {
        return "dateFormatChinese";
    }
}
